from django.shortcuts import render,redirect
from myApp.models import Users,Comments,Movies
from utils.error import *
# Create your views here.
from utils.getPublicData import *
from utils.getChartData import *
def home(request):
    try:
       username = request.session.get('username')
       userInfo = Users.objects.get(username=username)
       #getAllData()
       maxRate,maxTitle=getMaxRate()
       maxCommentlen=getMaxCommentlen()
       maxMovieTime=getMaxMovieTime()
       minTime=getMinTime()
       maxType,maxTypelen=getTypeAll()
       xData,yData=getAllRateData()
       userPieData=getAllUser()
       movieList=getAllMovies()
       defaultMovie=request.GET.get('movieName') or '惊变28年'
       movieInfo=getMovieInfoDetail(defaultMovie)
       print(defaultMovie)
    except:
        return redirect('login')
    return render(request,'index.html',{
        'userInfo':userInfo,
        'maxRate': maxRate,
        'maxTitle': maxTitle,
        'maxCommentlen': maxCommentlen,
        'maxMovieTime': maxMovieTime,
        'minTime': minTime,
        'maxType': maxType,
        'maxTypelen': maxTypelen,
        'xData': xData,
        'yData': yData,
        'userPieData': userPieData,
        'movieList': movieList,
        'movieInfo': movieInfo,
    })

def userInfo(request):
    try:
       username = request.session.get('username')
       userInfo = Users.objects.get(username=username)
       #getAllData()
       if request.method == 'POST':
           print(request.POST, request.FILES)
           changeSelfInfo(request.POST, request.FILES, username)
           userInfo = Users.objects.get(username=username)
    except:
        return redirect('login')
    return render(request,'userInfo.html',{
        'userInfo':userInfo,
    })

def changePassword(request):
    try:
       username = request.session.get('username')
       userInfo = Users.objects.get(username=username)
       #getAllData()
       if request.method == 'POST':
           print(request.POST)
           res=changePasswordTwo(username,request.POST)
           if res != None:
               return errorResponse(request,res)
           userInfo = Users.objects.get(username=username)
           # changeSelfInfo(request.POST, request.FILES, username)
           # userInfo = Users.objects.get(username=username)
    except:
        return redirect('login')
    return render(request,'changePassword.html',{
        'userInfo':userInfo,
    })

def tableData(request):
    try:
       username = request.session.get('username')
       userInfo = Users.objects.get(username=username)
       #getAllData()
       tableData=getAllData()

    except:
        return redirect('login')
    return render(request,'tableData.html',{
        'userInfo':userInfo,
        'tableData':tableData,
    })

def typeChart(request):
    try:
       username = request.session.get('username')
       userInfo = Users.objects.get(username=username)
       #getAllData()
       typePieData=getTypePieChatData()
       xdata,yData1,yData2=getTypeBarChatData()

    except:
        return redirect('login')
    return render(request,'typeChart.html',{
        'userInfo':userInfo,
        'typePieData':typePieData,
        'xdata':xdata,
        'yData1':yData1,
        'yData2':yData2,
    })

def countryChart(request):
    try:
       username = request.session.get('username')
       userInfo = Users.objects.get(username=username)
       #getAllData()
       countryX, countryY=getCountryChartData()
       languageDataEnd=getLangChartData()

    except:
        return redirect('login')
    return render(request,'countryChart.html',{
        'userInfo':userInfo,
        'countryX':countryX,
        'countryY':countryY,
        'languageDataEnd':languageDataEnd,
    })

def timeChart(request):
    try:
       username = request.session.get('username')
       userInfo = Users.objects.get(username=username)
       #getAllData()
       timeXNew, timeY1, timeY2=getPubTimeData()
       timeData=getMovieTimeLen()
    except:
        return redirect('login')
    return render(request,'timeChart.html',{
        'userInfo':userInfo,
        'timeXNew':timeXNew,
        'timeY1':timeY1,
        'timeY2':timeY2,
        'timeData':timeData
    })

def commentChart(request):
    try:
       username = request.session.get('username')
       userInfo = Users.objects.get(username=username)
       #getAllData()
       #getAllCommentsData()
       xData1, yData1=getAllHostData()
       xData, yData,result=getAllHostDetail()
       #getAllCommentsDataTwo()
    except:
        return redirect('login')
    return render(request,'commentChart.html',{
        'userInfo':userInfo,
        'xData1':xData1,
        'yData1':yData1,
        'xData':xData,
        'yData':yData,
        'result':result,
    })

def titleCloud(request):
    try:
       username = request.session.get('username')
       userInfo = Users.objects.get(username=username)

    except:
        return redirect('login')
    return render(request,'titleCloud.html',{
        'userInfo':userInfo,
    })

def summaryCloud(request):
    try:
       username = request.session.get('username')
       userInfo = Users.objects.get(username=username)

    except:
        return redirect('login')
    return render(request,'summaryCloud.html',{
        'userInfo':userInfo,
    })

def search(request):
    try:
       username = request.session.get('username')
       userInfo = Users.objects.get(username=username)
       #getAllData()
       if request.method == 'POST':
           movieInfos=''
           print(request.POST.get('searchWord'))
           movieInfos=getPostData(request.POST.get('searchWord'))
           return render(request, 'search.html', {
               'userInfo': userInfo,
               'movieInfos': movieInfos})
    except:
        return redirect('login')
    return render(request,'search.html',{
        'userInfo':userInfo,
    })

def movie(request):
    try:
       username = request.session.get('username')
       userInfo = Users.objects.get(username=username)
       print(request.GET.get('id'))
       movieInfo=getMovieById(request.GET.get('id'))
    except:
        return redirect('login')
    return render(request,'movie.html',{
        'userInfo':userInfo,
        'movieInfo':movieInfo,
    })

def login(request):
    if request.method == 'GET':
       return render(request,'login.html',{})
    else:
        username = request.POST.get('username')
        password = request.POST.get('password')
        try:
            user = Users.objects.get(username=username,password=password)
            print(username,password)
            request.session['username'] = user.username
            return redirect('home')
        except:
            return errorResponse(request,'请输入正确用户名密码！')

def logout(request):
    request.session.clear()
    return redirect('login')

def register(request):
    if request.method == 'GET':
        return render(request, 'register.html', {})
    else:
        username = request.POST.get('username')
        password = request.POST.get('password')
        checkPassword = request.POST.get('checkPassword')
        print(username,password,checkPassword)
        try:
            Users.objects.get(username=username)
        except:
            if not username or not password or not checkPassword:return errorResponse(request,'不允许为空')
            if password != checkPassword: return errorResponse(request,'两次密码不相符')
            Users.objects.create(username=username,password=password)
            return redirect('/myApp/login')
        return errorResponse(request,'该用户名已经注册')

